﻿/** 
 * Project Name:jdplat 
 * File Name:ServiceAop.java 
 * Package Name:com.jdplat.jdm.core.aop 
 * Date:2016年12月14日下午2:27:24 
 * Copyright (c) 2016, 948434963@qq.com All Rights Reserved. 
 * 
*/  
  
package com.jdplat.jdm.core.aop;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.jdplat.jdm.core.entity.MethodLog;
import com.jdplat.jdm.core.web.controller.BaseController;

/** 
 * ClassName:ServiceAop <br/> 
 * Function: 利用spring aop 实现方法操作记录. <br/> 
 * Reason:   定义参考 http://www.cnblogs.com/yanjunwu/p/3996570.html. <br/> 
 * Date:     2016年12月14日 下午2:27:24 <br/> 
 * @author   qirp
 * @version   
 * @since    JDK 1.7 
 * @see       
 */
@Component
@Aspect 
public class ServiceAop extends BaseController<MethodLog, Integer>{
	
	//主意：1、在spring配置文件applicationContext.xml加入<aop:aspectj-autoproxy />
	//     2、在springmvc配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
	
	
	
	
	@Pointcut("execution(* com.jdplat.jdm.web.service..*(..))")  
    public void makeOneCake() {  
    }  
  
    @Before("makeOneCake()")
    public void before(JoinPoint joinPoint){
    }  
    
    @After("makeOneCake()")
    public void after(JoinPoint joinPoint){  
    }  
  
  	@AfterReturning("makeOneCake()")
  	public void afterReturn(JoinPoint joinPoint){
  	}
  	
  	@AfterThrowing(pointcut="makeOneCake()", throwing="ex")
  	public void afterThrow(JoinPoint joinPoint, Exception ex){
  	}
  	
    @Around(value = "makeOneCake()")  
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {  
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        long startTime = System.currentTimeMillis();  
        Object object = joinPoint.proceed();  
        long endTime = System.currentTimeMillis();  
        
        MethodLog method = new MethodLog();
        method.setClss(targetName);
        method.setMethod(methodName);
        method.setTime((endTime - startTime) + "");
        method.setCreateTime(new Date());
        insert("request_space.insert_request_info", method);//持久化到数据库
        return object;	
    }  
}
 